package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.bean.GoodsDealerBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GoodsDealerMapper;
import com.molichuxing.services.infrastructure.dto.response.GoodsDealerDto;
import com.molichuxing.services.infrastructure.service.GoodsAuthService;
import com.molichuxing.services.property.GoodsDealerStatusEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商品分销 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-08-12
 */
@Service("goodsAuthService")
public class GoodsAuthServiceImpl implements GoodsAuthService {

    private static final Logger log = Logger.getLogger(GoodsAuthServiceImpl.class);

    @Resource
    private GoodsDealerMapper goodsDealerMapper;

    @Override
    public Paged<GoodsDealerDto> getDealerPage(Integer goodsId, List<Integer> dealerIds, GoodsDealerStatusEnum goodsDealerStatusEnum, int pageNum, int pageSize) {
        log.info("根据商品ID获取未授权和已授权的经销商。goodsId ： ["+ goodsId +"]"+ "goodsId:["+goodsId+"]"+"goodsDealerStatusEnum:["+ goodsDealerStatusEnum +"]");
        Integer goodsDealerStatus = goodsDealerStatusEnum == null ? null:goodsDealerStatusEnum.getValue();
        int count = goodsDealerMapper.getDealerPageCount(goodsId, dealerIds, goodsDealerStatus);
        if(count <= 0) {
            return new Paged<>();
        }
        log.info("根据商品ID获取未授权和已授权的经销商 入参：queryWrapper： "+ JSON.toJSONString(goodsId));
        List<GoodsDealerBean> result = goodsDealerMapper.getDealerPage(goodsId, dealerIds, goodsDealerStatus, pageNum, pageSize);
        log.info("根据商品ID获取未授权和已授权的经销商 出参：result： "+ JSON.toJSONString(result));
        return new Paged<>(Convert.toGoodsProductDealerBeanDto(result), count, pageSize );
    }

    @Override
    public List<GoodsDealerDto> getDealerList(Integer goodsId, List<Integer> dealerId, GoodsDealerStatusEnum goodsDealerStatusEnum) {
        log.info("根据条件查询商品列表。goodsId ： ["+ goodsId +"]"+ "goodsId:["+goodsId+"]"+"goodsDealerStatusEnum:["+ goodsDealerStatusEnum +"]");
        Integer goodsDealerStatus = goodsDealerStatusEnum == null ? null:goodsDealerStatusEnum.getValue();
        List<GoodsDealerBean> pageResult = goodsDealerMapper.getDealerList(goodsId, dealerId, goodsDealerStatus);
        log.info("根据商品ID获取未授权和已授权的经销商 出参：pageResult： "+ JSON.toJSONString(pageResult));
        return Convert.toGoodsProductDealerBeanDto(pageResult);
    }

    @Override
    public Boolean settingGoodsDistribution(Integer goods, Integer dealerId, GoodsDealerStatusEnum goodsDealerStatusEnum) {
        Integer goodsDealerStatus = goodsDealerStatusEnum == null ? null:goodsDealerStatusEnum.getValue();
        int result = goodsDealerMapper.settingGoodsDistribution(goods, dealerId, goodsDealerStatus);
        if(result > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean createGoodsDealer(List<Integer> goodsIds, List<Integer> dealerIds) throws Exception{
        if(null == goodsIds||null == dealerIds) {
            throw new Exception("参数错误");
        }
        for(Integer var : dealerIds) {
            for(Integer goodsId : goodsIds) {
                int count = goodsDealerMapper.getGoodsCount(Arrays.asList(goodsId), var);
                if(count != 0) {
                    goodsDealerMapper.deleteDealerRelation(var, Arrays.asList(goodsId));
                }
                GoodsDealerBean goodsDealerBean = new GoodsDealerBean();
                goodsDealerBean.setGoodsId(goodsId);
                goodsDealerBean.setDealerId(var);
                goodsDealerBean.setStatus(GoodsDealerStatusEnum.OPEN.getValue());
                int result = goodsDealerMapper.create(goodsDealerBean);
                if(result < 0) {
                    throw new Exception("分销经销商异常");
                }
            }

        }
        return true;
    }

    @Override
    public int deleteDealerRelation(Integer dealerId, List<Integer> goodsIds) {
        return goodsDealerMapper.deleteDealerRelation(dealerId, goodsIds);
    }
}
